/**
 * 添加装车站
 * @author xiaoxujs
 */

import { Form, Input, message, Modal, Switch } from "antd";
import { FC, useEffect } from "react";
import { ITlplTLaneLaneAddForm, ITlplTLaneLaneAddParams, ITlplTLaneLaneUpdate, ITlplTLaneList } from "../../../../../type/remoteControl/loadingControlList";
import { postTlplTLaneLaneAdd, postTlplTLaneLaneUpdate } from "../../../api";

const AddModule: FC<{
    open: boolean,
    handleAddCallback: () => void;
    clickData?: ITlplTLaneList; // 点击编辑的数据
}> = ({
    open,
    handleAddCallback,
    clickData,
}) => {
        const [form] = Form.useForm<ITlplTLaneLaneAddForm>();

        useEffect(() => {
            if(clickData) {
                form.setFieldsValue({
                    laneName: clickData.laneName,
                    laneState: clickData.laneState === 0,
                })
            } 
        }, [clickData, form]  )

        const handleOk = async () => {
            const values = await form.validateFields();
            if(clickData) {
                let params: ITlplTLaneLaneUpdate =  {
                    id: clickData.id,
                    laneName: values.laneName,
                    laneState: values.laneState ? 0 : 1,
                }
                const { code, msg } = await postTlplTLaneLaneUpdate(params) as { code: number, msg: string };
                if (code === 20000) {
                    message.success('编辑成功');
                    handleCancel(); 
                }else{
                    message.error(msg);
                }
            }else{
                let params: ITlplTLaneLaneAddParams = {
                    laneName: values.laneName,
                    laneState: values.laneState ? 0 : 1,
                }
    
                const { code, msg } = await postTlplTLaneLaneAdd(params) as { code: number, msg: string };
                if (code === 20000) {
                    message.success('添加成功');
                    handleCancel();
    
                } else {
                    message.error(msg);
                }
            }
      
            // postYckzZyLoadControlAddLoad
        };

        const handleCancel = () => {
            form.resetFields();
            handleAddCallback();
        };
        return <Modal title={clickData ? '编辑装车站': "添加装车站"}  open={open} onOk={handleOk} onCancel={handleCancel}>
            <Form
                labelCol={{ span: 6 }}
                wrapperCol={{ span: 16 }}
                form={form}
                initialValues={{
                    laneState: true,
                }}
            >
                <Form.Item<ITlplTLaneLaneAddForm>
                    label="装车站名称"
                    name="laneName"
                    rules={[{ required: true, message: '请输入装车站名称!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<ITlplTLaneLaneAddForm>
                    label="使用状态"
                    name="laneState"
                >
                    <Switch />
                </Form.Item>
            </Form>
        </Modal>
    }

export default AddModule;